package com.road.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.road.model.Msg;
import com.road.model.See;
import com.road.service.INewsService;
import com.road.service.ISeeService;
import com.road.service.imp.NewsServiceImp;
import com.road.service.imp.SeeServiceImp;
import com.road.utils.Consists;
import com.road.utils.ResponseUtil;

/**
 * Servlet implementation class SeeServlet
 */
@WebServlet(name = "see", description = "浏览量记录", urlPatterns = { "/see" })
public class SeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ISeeService iService=new SeeServiceImp();
	INewsService iNewsService=null;
    List<Object> list2=null;
    Msg msg=null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		list2=new ArrayList<>();
		msg=new Msg();
		List<See> list=null;
		Map<String, Object> map=null;
		try {
	        //更新浏览次数	1，获取浏览人员信息2	
			if(request.getParameter("action").equals("1")) {
				int times = iService.saveSeeTimes(request.getParameter("phone"), request.getParameter("news_id"));
				 if(times>0) {
					   iNewsService=new NewsServiceImp();
						msg.setCode(Consists.Error_Code_OK);
						msg.setMsg(Consists.Error_Msg_HANDLER_OK);
						map=new HashMap<>();
						map.put("see_times", iNewsService.getSeeTimesOrCommentOrSeeByNewsIds("see_count",request.getParameter("news_id")));
					}else {
						msg.setCode(Consists.Error_Code_FAIL);
						msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
					}
			}else if (request.getParameter("action").equals("2")) {
				list=iService.getSeePopleListByNewsId(request.getParameter("news_id"));
				if(list.size()>0) {
					msg.setCode(Consists.Error_Code_OK);
					msg.setMsg(Consists.Error_Msg_HANDLER_OK);
				}else {
					msg.setCode(Consists.Error_Code_FAIL);
					msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
				}
				list2.add(msg);
				list2.add(list);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
			list2.add(msg);
		}
		
		ResponseUtil.sendScript(response, JSON.toJSONString(list2));
	}

	

}
